<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    append-to-body
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible="visible"
    width="600px"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="登录类型" prop="type">
        <el-radio-group v-model="dataForm.type" @input="handleTypeChanged">
          <el-radio label="1" border>员工信息登录</el-radio>
          <el-radio label="2" border>特惠码登录</el-radio>
        </el-radio-group>
      </el-form-item>
      <template v-if="dataForm.type == '1'">
        <el-form-item label="工号" prop="userNo">
          <el-input
            v-model="dataForm.userNo"
            placeholder="请输入工号"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="dataForm.phone"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="userName">
          <el-input
            v-model="dataForm.userName"
            placeholder="请输入姓名"
          ></el-input>
        </el-form-item>
      </template>

      <template v-else>
        <el-form-item label="特惠码" prop="couponCode">
          <el-input
            v-model="dataForm.couponCode"
            placeholder="请输入特惠码"
          ></el-input>
        </el-form-item>
      </template>
      
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit"
        >确定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj } from "@/api/shop_user/user_inner";

export default {
  props:{
    visible: {
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      canSubmit: false,
      dataForm: {
        type:'1'
      },
      dataRule: {
        userNo: [
          { required: true, message: "登录类型不能为空", trigger: "blur" },
        ],
        userNo: [
          { required: true, message: "工号不能为空", trigger: "blur" },
        ],
        phone: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
        ],
        couponCode: [
          { required: true, message: "特惠码不能为空", trigger: "blur" },
        ],
      },
    };
  },
  components: {
  },
  methods: {
    init(id) {
      this.canSubmit = true;
      if(id){
        getObj(id).then((response) => {
          this.dataForm = response.data.data;
        });
      }
      
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {
            putObj(this.dataForm)
              .then((data) => {
                this.$notify.success("修改成功");
                this.$emit("refreshDataList");
                this.closeDialog()
              })
              .catch(() => {
                this.canSubmit = true;
              });
          } else {
            addObj(this.dataForm)
              .then((data) => {
                this.$notify.success("添加成功");
                this.$emit("refreshDataList");
                this.closeDialog()
              })
              .catch(() => {
                this.canSubmit = true;
              });
          }
        }
      });
    },
    //重置表单
    closeDialog() {
      this.$emit('update:visible', false)
    },

    /**
     * 切换登录类别 
     */
    handleTypeChanged(val){
      console.log(val)
      this.$refs["dataForm"].clearValidate()
    }
  },
};
</script>
